#include "TcpServer.hpp"
#include <memory>
#include "Protocol.hpp"
#include " NetCal.hpp"
#include " Daemon.hpp"
void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " port" << std::endl;
}

// ./tcpserver 8080
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }

    //守护进程，创建独立的会话，后台运行
    //Daemon(0, 0); //自主实现
    daemon(0,0);//系统调用
    // 1.顶层
    std::unique_ptr<Cal> cal = std::make_unique<Cal>();

    // 2.协议层
    std::unique_ptr<Protocol> protocol = std::make_unique<Protocol>([&cal](Request req) -> Response
                                                                    { return cal->Execute(req); });
    // 3.服务器层
    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(std::stoi(argv[1]), [&protocol](std::shared_ptr<Socket> &sock, InetAddr &client)
                                                                  { protocol->GetRequest(sock, client); });

    tsvr->start();
    return 0;
}